package com.six.service;

import com.six.domain.vo.DishSaleRankVo;
import com.six.domain.vo.PayChannelStatVo;
import com.six.domain.vo.SalesStatVo;
import com.six.mapper.ReportMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ReportService {

    private final ReportMapper reportMapper;

    public List<SalesStatVo> salesStat(Long storeId,
                                       LocalDateTime start,
                                       LocalDateTime end) {
        return reportMapper.salesDaily(storeId, start, end);
    }

    public List<DishSaleRankVo> dishRank(Long storeId,
                                         LocalDateTime start,
                                         LocalDateTime end,
                                         int top) {
        return reportMapper.dishRank(storeId, start, end, top);
    }

    public List<PayChannelStatVo> payChannel(Long storeId,
                                             LocalDateTime start,
                                             LocalDateTime end) {
        List<PayChannelStatVo> list = reportMapper.payChannel(storeId, start, end);
        BigDecimal total = list.stream()
                .map(PayChannelStatVo::getAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        list.forEach(l -> l.setPercent(
                total.compareTo(BigDecimal.ZERO) == 0
                        ? BigDecimal.ZERO
                        : l.getAmount()
                        .divide(total, 4, RoundingMode.HALF_UP)
                        .multiply(BigDecimal.valueOf(100))
                        .setScale(2, RoundingMode.HALF_UP)));
        return list;
    }
}
